<?php
namespace Admin\Controller;

use Think\Controller;

class JobInfoController extends CommonController
{
    /*
	 * 方法: index
	 * 功能：职位列表
	**/
    public function index()
    {
        $ptitle='招聘信息管理';
        $title='首页';
        $row = 10;
        $page = $_GET['p'];
        if ($page == '') {
            $page = 1;
        }
        $where = array(
            'status' => 1,
        );
        $job_list = M('job_info')
            ->where($where)
            ->page($page, $row)
            ->order('sort asc')
            ->select();
        $job_num =  M('job_info')
            ->where($where)
            ->count();
        $Page = new \Think\Page($job_num, $row);
        $show = $Page->show();
        $this->assign('ptitle', $ptitle);
        $this->assign('title', $title);
        $this->assign('page', $show);
        $this->assign('job_list', $job_list);
        $this->display();
    }

    /*
    * 方法: add
    * 功能：职位添加
   **/
    public function add()
    {
        if(!IS_POST){
            $ptitle='招聘信息管理';
            $title='添加';
            $this->assign('ptitle', $ptitle);
            $this->assign('title', $title);
            $this->display();
        }else{
            $data['job_name'] = I('job_name');
            $data['job_describe'] = I('job_describe');
            $data['status'] = I('status');
            $data['sort'] = I('sort');
            $data['add_time'] = date('Y-m-d H:i:s',time());
            $addResult = M('job_info')->add($data);
            if($addResult){
                echo 1;
            }else{
                echo 0;
            }
        }
    }

    /*
    * 方法: edit
    * 功能：职位修改
    * 参数：job_id，职位ID
   **/
    public function edit()
    {
        if(!IS_POST){
            $ptitle = '招聘信息管理';
            $title = '修改';
            $job_id = I('job_id');
            //查询该文章分类信息
            $job_info = M('job_info')
                ->where(array('job_id' => $job_id))
                ->find();
            //查询所有一级分类
            $this->assign('ptitle', $ptitle);
            $this->assign('title', $title);
            $this->assign('job_info', $job_info);
            $this->assign('job_id', $job_id);
            $this->display();
        }else{
            $data['job_id'] = I('job_id');
            $data['job_name'] = I('job_name');
            $data['job_describe'] = I('job_describe');
            $data['status'] = I('status');
            $data['order'] = I('order');
            $data['add_time'] = date('Y-m-d H:i:s',time());
            $editResult = M('job_info')->save($data);
            if($editResult !== false){
                echo 1;
            }else{
                echo 0;
            }
        }
    }

    /*
   * 方法: edit
   * 功能：职位的删除
   * 参数：job_id，职位ID
  **/
    public function del()
    {
        $ids = I('ids');
        if(is_array($ids))
        {
            $ids = implode(',',$ids);
        }
        $map = array(
            'job_id' => array(
                'in',"$ids"
        ));
        if (M('job_info ')->where($map)->delete() !== false) {
            $this->success('职位删除成功！');
        } else {
            $this->error('职位删除失败！');
        }
    }
}